from django.db import models
from django.contrib.auth.models import User

class Message(models.Model):
    sender = models.ForeignKey('user.User', related_name='sent_messages', on_delete=models.CASCADE, verbose_name='发送者')
    receiver = models.ForeignKey('user.User', related_name='received_messages', on_delete=models.CASCADE, verbose_name='接收者')
    content = models.TextField(verbose_name='消息内容')
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name='发送时间')
    is_read = models.BooleanField(default=False, verbose_name='是否已读')
    room_id = models.CharField(max_length=64, verbose_name='房间号', db_index=True, null=True, blank=True)

    class Meta:
        db_table = 'message'
        ordering = ['timestamp']
        verbose_name = '私聊消息'
        verbose_name_plural = '私聊消息'

    def __str__(self):
        return f'{self.sender} -> {self.receiver}: {self.content[:20]}'

